﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2018-09-05 12:02:25 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;

namespace MTQ.Web.Controllers
{

    public class Base_UserRoleController : BaseController
    {

        private IBase_UserRoleService ibase_userroleService = new Base_UserRoleService();
        private IBase_RoleService ibase_roleService = new Base_RoleService();


        [AccountAuth(Roles = "Base_UserRole_Config")]
        public ActionResult Config(Guid? primarykey)
        {
            ViewBag.UserId = primarykey;

            return View();
        }
        [HttpPost]
        [AccountAuth(Roles = "Base_UserRole_GetRole")]
        public ActionResult GetRole(Guid? primarykeys)
        {
            List<TreeView> listMsg = new List<TreeView>();
            List<Base_Role> listAllRole = ibase_roleService.LoadEntities(x => true).ToList();
            List<Base_UserRole> listRoleRole = ibase_userroleService.LoadEntities(x => x.Base_UserId == primarykeys).ToList();

            listMsg = GetRoleList(listAllRole.Where(x => x.Role_Available == "Y").ToList());

            GetUserRole(listRoleRole, listMsg);

            return Json(listMsg);
        }
        private List<TreeView> GetRoleList(List<Base_Role> listCurrentRole)
        {
            if (listCurrentRole == null || listCurrentRole.Count == 0)
            {
                return null;
            }
            List<TreeView> listMsg = new List<TreeView>();
            //父节点
            foreach (var item in listCurrentRole)
            {
                TreeView tv = new TreeView()
                {
                    a_attr = new A_attrs() { Role_Id = item.Role_Id },
                    text = item.Role_Name,
                };
                listMsg.Add(tv);
            };

            return listMsg;
        }
        private TreeViewState treeViewStateSelected = new TreeViewState() { selected = true };
        private void GetUserRole(List<Base_UserRole> listRoleAuth, List<TreeView> listMsg)
        {
            foreach (var item in listMsg)
            {
                if (listRoleAuth.Where(x => x.Base_RoleId == item.a_attr.Role_Id).Count() > 0)
                {
                    item.state = treeViewStateSelected;
                }
            }
        }

        [HttpPost]
        [AccountAuth(Roles = "Base_UserRole_EditRole")]
        public ActionResult EditRole(Guid primarykeys, string Roleid)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            // List<string> arrNewRole = Roleid.Split(',').ToList();
            List<Guid> newRoleList = Roleid.Split(',').ToList().ConvertAll<Guid>(x => new Guid(x));
            List<Base_UserRole> listRoleRole = ibase_userroleService.LoadEntities(x => x.Base_UserId == primarykeys).ToList();

            List<Base_UserRole> listDeleteId = new List<Base_UserRole>();
            List<Base_UserRole> listAddId = new List<Base_UserRole>();

            //获取删除数据
            foreach (var item in listRoleRole)
            {
                if (!newRoleList.Contains(item.Base_RoleId))
                {
                    listDeleteId.Add(item);
                }
            }

            //获取新增的数据
            List<Guid> arrOldRole = listRoleRole.Select(x => x.Base_RoleId).ToList();
            foreach (Guid item in newRoleList)
            {
                if (!arrOldRole.Contains(item))
                {
                    Base_UserRole roleRole = new Base_UserRole()
                    {
                        UserRole_Id = System.Guid.NewGuid(),
                        Base_RoleId = item,
                        Base_UserId = primarykeys
                    };
                    listAddId.Add(roleRole);
                }

            }
            ibase_userroleService.AddListEntity(listAddId);
            ibase_userroleService.DeleteListEntity(listDeleteId);
            return Json(listMsg);
        }




    }
}




